<?Lassoscript
// Last modified 8/7/11 by ECL, Landmann InterActive

// FUNCTIONALITY
// Include for content add response page

// CHANGE NOTES
// 10/12/07
// Recoded for CMS v. 3.0
// 11/15/07
// Added five new fields for SEO tailoring: Keywords, Author, Description, Copyright, Title
// Switched fields CategoriesID to HeirarchyID, removed SectionID as it is now obsolete
// 4/2/08
// Added code for URL Fix (Issue #547)
// 1/15/09
// Added Story field
// 6/22/09
// Added gallery feature
// 8/25/09
// Adding page stylesheet
// 11/24/09
// Add Show in Navigation field
// 11/30/10
// Added "Duplicate" this page functionality
// 8/7/11
// Added Slider feature

// Debugging
// Var('svDebug' = 'Y');

// Converting action_params
Var('vError' = '');
Var('vOption' = '');
// The action_param "Action" is passed by the frm_content.inc form to indicate whether it is an add or edit
// Action = Update, means the request was an Edit (from frm_content.inc)
// Action = Add, means the request was an Add (from frm_content.inc)
// Action = Duplicate, means the request was to duplicate an existing page (from frm_content.inc). Treated same as "add".
Var('vAction' = Action_Param('Action'));

If($svDebug == 'Y');
	'<br>\n';
	'<b>content_addresponse.inc</b><br>\n';
/If;

// Checking referrer to make sure it's only posting from the Add page
// ============= ADD ========
If( ($vAction == 'Add') || ($vAction == 'Duplicate') );

	// Convert action_params
	Include(($svAdminHelpersPath)+'content_params.inc');

	// If an error was generated from the Params, no point continuing, so bail immediately
	If($vError == '');

		// Construct the query to add the record
		Var('SQLQueryAddContent' = (StripBackticks('/* SQLQueryAddContent */
		INSERT INTO '+($svSiteDatabase)+'.'+($svContentTable)+
			' SET
				HeirarchyID="' 		+(Encode_SQL($vHeirarchyID))+'",
				Headline="'	 		+(Encode_SQL($vHeadline))+'",
				URLSlug="'	 		+(Encode_SQL($vURLSlug))+'",
				Subhead="' 			+(Encode_SQL($vSubhead))+'",
				Author="' 			+(Encode_SQL($vAuthor))+'",
				Text_1="' 			+(Encode_SQL($vText_1))+'",
				Text_2="'			+(Encode_SQL($vText_2))+'",
				Image_thumb="'		+(Encode_SQL($vImage_thumb))+'",
				Image_med="'		+(Encode_SQL($vImage_med))+'",
				Image_large="'		+(Encode_SQL($vImage_large))+'",
				Image_title="'		+(Encode_SQL($vImage_title))+'",
				Image_caption="'	+(Encode_SQL($vImage_caption))+'",
				Media_File="'		+(Encode_SQL($vMedia_File))+'",
				Price_1="'			+(Encode_SQL($vPrice_1))+'",
				Price_2="'	 		+(Encode_SQL($vPrice_2))+'",
				URL="'				+(Encode_SQL($vURL))+'",
				PageKeywords="'		+(Encode_SQL($vPageKeywords))+'",
				PageAuthor="'		+(Encode_SQL($vPageAuthor))+'",
				PageDescription="'	+(Encode_SQL($vPageDescription))+'",
				PageCopyright="' 	+(Encode_SQL($vPageCopyright))+'",
				PageTitle="'  		+(Encode_SQL($vPageTitle))+'",
				PageTemplate="' 	+(Encode_SQL($vPageTemplate))+'",
				PageStylesheet="' 	+(Encode_SQL($vPageStylesheet))+'",
				PortfolioGroupID="'	+(Encode_SQL($vPortfolioGroupID))+'",
				GalleryGroupID="' 	+(Encode_SQL($vGalleryGroupID))+'",
				SliderGroupID="' 	+(Encode_SQL($vSliderGroupID))+'",
				Testimonial="'	 	+(Encode_SQL($vTestimonial))+'",
				Story="'		 	+(Encode_SQL($vStory))+'",
				SortOrder="'		+(Encode_SQL($vSortOrder))+'",
				Active="'			+(Encode_SQL($vActive))+'",
				ActiveFrom="'		+(Encode_SQL($vActiveFrom))+'",
				ActiveTo="'			+(Encode_SQL($vActiveTo))+'",
				ShowInNav="'		+(Encode_SQL($vShowInNav))+'",
				AttachPopup="'		+(Encode_SQL($vAttachPopup))+'",
				DateCreated="'		+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'",
				DateModified="'		+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'";
				SELECT LAST_INSERT_ID() AS NewContentID;')));
			
			If($svDebug == 'Y');
				('128: SQLQueryAddContent = '+($SQLQueryAddContent)+'<br>\n');
			/If;
		
			Inline($IV_Content, -Table=$svContentTable, -SQL=$SQLQueryAddContent);
				
				If((Error_CurrentError)==(Error_NoError));
			
					// If no error, Add was successful, dump out Error 1001 "Add Successful"
					// Copying ContentID to vNewID to use in links
					$vError = '1001';
					$vOption = Var('vHeadline');
					Var('vNewID' = Field('NewContentID'));
					If($svDebug == 'Y');
						('137: vError = '+($vError)+'<br>\n');
					/If;
			
				// There was an error
				Else;
					$vError = (Error_CurrentError);
				/If;
			
			/Inline;

	/If;

// ============= EDIT ========
// Action = Update, means the request came from the Edit page
Else($vAction == 'Update');

	// Convert action_params
	Include(($svAdminHelpersPath)+'content_params.inc');
	Var('vID' = '');
	Var('vID' = Action_Param('id'));

	// Copying the ID to vNewID to build link
	// This is so we can just use vNewID at bottom of page for both add and edit
	Var('vNewID' = Var('vID'));

	// If an error was generated from the Params, no point continuing, so bail immediately
	If($vError == '');

		// Construct the query to Update the record
		// Not changing the Date_Created fields
		Var('SQLUpdateContent' = (StripBackticks('/* SQLUpdateContent */
		UPDATE '+($svContentTable)+' SET
			HeirarchyID="'	 	+(Encode_SQL($vHeirarchyID))+'",
			Headline="'	 		+(Encode_SQL($vHeadline))+'",
			URLSlug="'	 		+(Encode_SQL($vURLSlug))+'",
			Subhead="' 			+(Encode_SQL($vSubhead))+'",
			Author="' 			+(Encode_SQL($vAuthor))+'",
			Text_1="' 			+(Encode_SQL($vText_1))+'",
			Text_2="'			+(Encode_SQL($vText_2))+'",
			Image_thumb="'		+(Encode_SQL($vImage_thumb))+'",
			Image_med="'		+(Encode_SQL($vImage_med))+'",
			Image_large="'		+(Encode_SQL($vImage_large))+'",
			Image_title="'		+(Encode_SQL($vImage_title))+'",
			Image_caption="'	+(Encode_SQL($vImage_caption))+'",
			Media_File="'		+(Encode_SQL($vMedia_File))+'",
			Price_1="'			+(Encode_SQL($vPrice_1))+'",
			Price_2="'	 		+(Encode_SQL($vPrice_2))+'",
			URL="'				+(Encode_SQL($vURL))+'",
			PageKeywords="' 	+(Encode_SQL($vPageKeywords))+'",
			PageAuthor="'		+(Encode_SQL($vPageAuthor))+'",
			PageDescription="' 	+(Encode_SQL($vPageDescription))+'",
			PageCopyright="'  	+(Encode_SQL($vPageCopyright))+'",
			PageTitle="'  		+(Encode_SQL($vPageTitle))+'",
			PageTemplate="'  	+(Encode_SQL($vPageTemplate))+'",
			PageStylesheet="'  	+(Encode_SQL($vPageStylesheet))+'",
			PortfolioGroupID="'	+(Encode_SQL($vPortfolioGroupID))+'",
			GalleryGroupID="' 	+(Encode_SQL($vGalleryGroupID))+'",
			SliderGroupID="' 	+(Encode_SQL($vSliderGroupID))+'",
			Testimonial="'	 	+(Encode_SQL($vTestimonial))+'",
			Story="'		 	+(Encode_SQL($vStory))+'",
			SortOrder="'		+(Encode_SQL($vSortOrder))+'",
			Active="'			+(Encode_SQL($vActive))+'",
			ActiveFrom="'		+(Encode_SQL($vActiveFrom))+'",
			ActiveTo="'			+(Encode_SQL($vActiveTo))+'",
			ShowInNav="'		+(Encode_SQL($vShowInNav))+'",
			AttachPopup="'		+(Encode_SQL($vAttachPopup))+'",
			DateModified="'		+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'"
			WHERE ID = "'+($vID)+'"')));

		If($svDebug == 'Y');
			('224: SQLUpdateContent = '+($SQLUpdateContent)+'<br>\n');
		/If;
	
		Inline($IV_Content, -Table=$svContentTable, -SQL=$SQLUpdateContent);
			
			If((Error_CurrentError)==(Error_NoError));
		
				// If no error, Update was successful, dump out Error 1011 "Update Successful"
				$vError = '1011';
				$vOption = Var('vDisplayName');
				If($svDebug == 'Y');
					('233: vError = '+($vError)+'<br>\n');
				/If;
		
			// There was an error
			Else;
				$vError = (Error_CurrentError);
			/If;
		
		/Inline;

	// vError == ''
	/If;

// ============= DELETE ========
Else($vAction == 'Delete');

	// Convert action_params
	Var('vID' = Action_Param('ID'));
		
	// Build the Delete Query
	Var('SQLDeleteQuery' = string);
	Var('SQLDeleteQuery' = (('DELETE FROM '+($svSiteDatabase)+'.'+($svContentTable)+' WHERE ID = "'+($vID)+'"')));
	
	// Delete the record
	Inline($IV_Content, -SQL=$SQLDeleteQuery);
		// If delete successful, output 1021 "Deletion Successful" and go ahead and delete attachments,
		If(Error_Code == '0');
			$vError = '1021';
			$vOption = $vID;
		// If delete failed, output error 1022, "Delete Failed"
		Else;
			$vError = '1022';
			$vOption = Error_CurrentError;
			LI_URLRedirect(-Page=(($svAdminControllersPath)+($svAddResponsePage)),-ExParams=('DataType='+($vDataType)),-UseError='Y',-Error=$vError,-Option=$vOption);
		/If;
	/Inline;

		
// Action is wrong, somebody is messing with us
// Dump out error 9002, "Database Error"
Else;
	$vError = '9002';
/If;


// Deal with the results of the processing

// ============= ADD  ========
// If any other error other than 1001 "Add Successful" do a redirect
If(($vAction == 'Add') || ($vAction == 'Duplicate'));
	// Standard Error Table
	If($vError == '1001');
		LI_ShowError3(-ErrNum=$vError, -Option=$vOption);

// Edit New Listing Link -->
?>
<div align="left" style="margin-bottom:20px;">
	View the record for <a href="[$svAdminControllersPath][$svEditRecordPage]?Datatype=Content&ID=[Var('vNewID')]&New=Y"><b>[$vDisplayName]</b></a>
</div>
<?Lassoscript
	Else;
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svAddPage)),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y');
	/If;
/If;


// ============= EDIT ========
// If vAction = Edit, Always redirect to edit page
// If record updated OK, will get a 1011 "Update Successful" message, but do NOT pass the Params, only the ID
// This forces the system to do a new lookup
If($vAction == 'Update');
	If($vError == '1011');
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&ID='+($vNewID)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='N');
	Else;
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y');
	/If;
/If;

// ============= DELETE ========
// If Action = Delete, redirect to search page
If($vAction == 'Delete');
	LI_URLRedirect(-Page=(($svAdminControllersPath)+($svSearchPage)),-ExParams=('DataType='+($vDataType)),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='N');
/If;
?>
